package com.nd.qa.shareplate.script.appium;

import com.nd.qa.shareplate.common.SysConstant;
import com.nd.qa.shareplate.model.ElementType;
import com.nd.qa.shareplate.model.PageElement;

public class Appium {
	public String scriptInit(String modelName,String scriptName, String packagename, String activityName){
		String code = "package %s.test;\n"
				+ "import io.appium.java_client.AppiumDriver;\n"
				+ "import io.appium.java_client.android.AndroidDriver;\n"
				+ "import java.net.URL;\n"
				+ "import java.util.List;\n"
				+ "import org.junit.*;\n"
				+ "import org.openqa.selenium.WebElement;\n"
				+ "import org.openqa.selenium.remote.DesiredCapabilities;\n"
				+ "//%s\n"
				+ "public class %s {\n"
				+ "private AppiumDriver driver;\n"
				+ "@Before\n"
				+ "public void setUp() throws Exception {\n"
				+ "    DesiredCapabilities capabilities = new DesiredCapabilities();\n"
				+ "    capabilities.setCapability(\"deviceName\",\"Android Emulator\");\n"
				+ "    capabilities.setCapability(\"platformVersion\", \"4.4\");\n"
				+ "    capabilities.setCapability(\"appPackage\", \"%s\");\n"
				+ "    capabilities.setCapability(\"appActivity\", \"%s\");\n"
				+ "    capabilities.setCapability(\"unicodeKeyboard\", true);\n"
				+ "    capabilities.setCapability(\"resetKeyboard\", true);\n"
				+ "    driver = new AndroidDriver(new URL(\"http://127.0.0.1:4723/wd/hub\"), capabilities);\n"
				+ "}\n@After\n"
				+ "public void tearDown() throws Exception {\n"
				+ "    driver.closeApp();\n"
				+ "    driver.quit();\n"
				+ "}\n";
		return String.format(code, packagename,modelName, scriptName, packagename, activityName);
	}	    	
	
	private String findElement(PageElement element, String operate){
		String code = "";
		switch (element.getElementTypeName()) {
		case SysConstant.ELEMENT_TYPE_TEXT:
			code = "driver.findElementByName(\"%s\").%s;\n";
			break;
		case SysConstant.ELEMENT_TYPE_TAGNAME:
			code = "driver.findElementByTagName(\"%s\").%s;\n";
			break;
		case SysConstant.ELEMENT_TYPE_XPATH:
			code = "driver.findElementByXPath(\"%s\").%s;\n";
			break;
		case SysConstant.ELEMENT_TYPE_ID:
			code = "driver.findElementByAccessibilityId(\"%s\").%s;\n";
			break;
		case SysConstant.ELEMENT_TYPE_CLASSNAME:
			code = "driver.findElementByClassName(\"%s\").%s;\n";
			break;
		case SysConstant.ELEMENT_TYPE_CSSSELECTOR:
			code = "driver.findElementByCssSelector(\"%s\").%s;\n";
			break;
		case SysConstant.ELEMENT_TYPE_EDITTEXT:
			code = "driver.findElementByClassName(\"android.widget.%s\").%s;\n";
			break;
		case SysConstant.ELEMENT_TYPE_BUTTON:
			code = "driver.findElementByName(\"%s\").%s;\n";
			break;
		default:
			code = "driver.findElementByClassName(\"android.widget.%s\").%s;\n";
			break;
		}
		if (code.contains("findElementByClassName")) {
			if (element.getElementTypeName().equals(SysConstant.ELEMENT_TYPE_EDITTEXT)) {
				System.out.println("findElement=="+String.format(code, element.getElementTypeName(),operate));
				return String.format(code, element.getElementTypeName(),operate);
			}else{
				return String.format(code, element.getElementTypeName(),operate);
			}
		}else{
			return String.format(code, element.getElementContent(), operate);
		}
		/*System.out.println("findElement=="+String.format(code, element.getElementText(), operate));
		return String.format(code, element.getElementText(), operate);*/
	}
	
	private String findElements(PageElement element, String operate){
		String code = "";
		switch (element.getElementTypeName()) {
		case SysConstant.ELEMENT_TYPE_TEXT:
			code = "driver.findElementsByName(\"%s\").get(%s).%s;\n";
			break;
		case SysConstant.ELEMENT_TYPE_TAGNAME:
			code = "driver.findElementsByTagName(\"%s\").get(%s).%s;\n";
			break;
		case SysConstant.ELEMENT_TYPE_XPATH:
			code = "driver.findElementsByXPath(\"%s\").get(%s).%s;\n";
			break;
		case SysConstant.ELEMENT_TYPE_ACCESSIBILITYID:
			code = "driver.findElementsByAccessibilityId(\"%s\").get(%s).%s;\n";
			break;
		case SysConstant.ELEMENT_TYPE_ID:
			code = "driver.findElementsById(\"%s\").get(%s).%s;\n";
			break;
		case SysConstant.ELEMENT_TYPE_CLASSNAME:
			code = "driver.findElementsByClassName(\"%s\").get(%s).%s;\n";
			break;
		case SysConstant.ELEMENT_TYPE_CSSSELECTOR:
			code = "driver.findElementsByCssSelector(\"%s\").get(%s).%s;\n";
			break;
		case SysConstant.ELEMENT_TYPE_EDITTEXT:
			code = "driver.findElementsByClassName(\"android.widget.%s\").get(%s).sendKeys(%s);\n";
			break;
		case SysConstant.ELEMENT_TYPE_BUTTON:
			code = "driver.findElementsByName(\"%s\").get(%s).%s;\n";
			break;
		default:
			code = "driver.findElementsByClassName(\"android.widget.%s\").get(%s).%s;\n";
		}
		if (code.contains("findElementsByClassName")) {
			if (element.getElementTypeName().equals(SysConstant.ELEMENT_TYPE_EDITTEXT)) {//input输入框特殊处理
				System.out.println(String.format(code, element.getElementTypeName(),element.getElementOrder(),element.getElementText(),operate));
				return String.format(code, element.getElementTypeName(),element.getElementOrder(),element.getElementText(),operate);
			}else{
				return String.format(code, element.getElementTypeName(), element.getElementOrder(), operate);
			}
		}else{
			return String.format(code, element.getElementContent(), element.getElementOrder(), operate);
		}
	}
	
	public String click(PageElement element){
		if (element.getElementOrder() > 0) {
			return findElements(element, "click()");
		}
		return findElement(element, "click()");
	}
	
	public String sleep(int time){ 
		return String.format("Thread.sleep(%d);\n", time);
	}

	public String drag(PageElement element) {
		if (element.getElementOrder() > 0) {
			return findElements(element, "drag()"); 
		}
		return findElement(element, "drag()");
	}
	public void swipe(int startx, int starty, int endx, int endy, int duration){
		
	}
	public String enter(PageElement element) {
		// TODO Auto-generated method stub
		//判断页面上是否存在多个同样的元素类型，是:findElements;否:findElement
		if (element.getElementOrder() > 0) {
			return findElements(element, "sendKeys(\"" + element.getElementText() + "\")");
		}
		return findElement(element, "sendKeys(\"" + element.getElementText() + "\")");
	}
}
